package com.cljz.makepassenger.ui.activity;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.event.UserInfoChangeEvent;
import com.cljz.makepassenger.present.activity.NicknameEditP;
import com.cljz.makepassenger.utils.date.TextUtil;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/12.
 */

public class NicknameEditActivity extends BaseActivity<NicknameEditP> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.rl_save)
    RelativeLayout rlSave;
    @BindView(R.id.et_nickname)
    EditText etNickname;
    @BindView(R.id.ll_main)
    LinearLayout llMain;

    @Override
    public void initData(Bundle savedInstanceState) {
        String nickname = getP().getLocalUserInfo(getP().getToken()).getUser_nickname();
        etNickname.setText(nickname);
        etNickname.setSelection(nickname.length());

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_nickname_edit;
    }

    @Override
    public NicknameEditP newP() {
        return new NicknameEditP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(rlBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        finish();
                    }
                });
        RxView.clicks(rlSave)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        hideSoftKeyboard();
                        String nickname = etNickname.getText().toString().trim();
                        if (TextUtil.isEmpty(nickname)) {
                            showToast("昵称不能为空", R.mipmap.jinshi);
                        } else {
                            Map<String, String> userInfoMap = new HashMap<String, String>();
                            userInfoMap.put("token", getP().getToken());
                            userInfoMap.put("user_nickname", etNickname.getText().toString().trim());
                            getP().getUserMsgUp(userInfoMap);
                        }
                    }
                });
        RxTextView.textChanges(etNickname)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() > 0) {
                            tvSave.setTextColor(getResources().getColor(R.color.blue_450));
                            rlSave.setClickable(true);
                        } else {
                            tvSave.setTextColor(getResources().getColor(R.color.blue_480));
                            rlSave.setClickable(false);
                        }
                    }
                });
    }

    public void userMsgUpFail(String error) {
        showToast(error, R.mipmap.jinshi);
        Router.pop(this);
    }

    public void userMsgUpSucceed(String msg) {
        showToast(msg, R.mipmap.nearby_success);
        BusProvider.getBus().post(new UserInfoChangeEvent());
        Router.pop(this);
    }
}
